package com.jffccloud.auth.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Autowired
    private AccessDeniedHandler pcAccessDeniedHandler;
    @Autowired
    private OAuth2WebSecurityExpressionHandler pcSecurityExpressionHandler;
    @Autowired
    protected AuthenticationSuccessHandler pcAuthenticationSuccessHandler;

    @Autowired
    protected AuthenticationFailureHandler pcAuthenticationFailureHandler;
    @Autowired
    private OauthSecurityConfig oauthSecurityConfig;
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()// 表单登录  来身份认证
                .loginPage("/myLogin.html")// 自定义登录页面
                .loginProcessingUrl("/authentication/form")// 自定义登录路径
                .successHandler(pcAuthenticationSuccessHandler)
                .failureHandler(pcAuthenticationFailureHandler);
        http.apply(oauthSecurityConfig)
                .and()
                .authorizeRequests()
                .antMatchers("/myLogin.html","/authentication/form","/**/hystrix.stream").permitAll()// 这些页面不需要身份认证,其他请求需要认证
                .and()
                .authorizeRequests()
                .antMatchers(
                        "/social/**",
                        "/**/*.js",
                        "/*.html",
                        "/**/*.css",
                        "/**/*.jpg",
                        "/**/*.png",
                        "/**/*.woff2",
                        "/code/image")
                .permitAll()//以上的请求都不需要认证
                .anyRequest() // 任何请求
                .authenticated()//; // 都需要身份认证
                .and()
                .headers().frameOptions().disable()
                .and()
                .exceptionHandling().accessDeniedHandler(pcAccessDeniedHandler)
                .and()
                .csrf().disable();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.expressionHandler(pcSecurityExpressionHandler);
    }
}
